package org.edu.modules.classChoise.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.classChoise.entity.VeJwXkms;
import org.edu.modules.classChoise.service.IVeJwXkmsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

 /**
 * @Description: 选课模式
 * @Author:
 * @Date:   2021-05-14
 * @Version: V1.0
 */
@Api(tags="选课模式")
@RestController
@RequestMapping("/classChoise/veJwXkms")
@Slf4j
public class VeJwXkmsController extends BaseController<VeJwXkms, IVeJwXkmsService> {
	@Autowired
	private IVeJwXkmsService veJwXkmsService;
	
	/**
	 * 分页查询
	 * @param veJwXkms
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "选课模式-分页查询")
	@ApiOperation(value="分页查询", notes="选课模式-分页查询")
	@PostMapping(value = "/list")
	public Result<?> queryPageList(VeJwXkms veJwXkms,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
	    if(veJwXkms.getIsdelete()==null){
	        veJwXkms.setIsdelete(0);
        }
        QueryWrapper<VeJwXkms> queryWrapper = QueryGenerator.initQueryWrapper(veJwXkms, req.getParameterMap());
        Page<Map<String,Object>> page = new Page<Map<String,Object>>(pageNo, pageSize);
        IPage<Map<String,Object>> pageList = veJwXkmsService.pageMaps(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	* 通过id查询
	* @param id
	* @return
	*/
	@AutoLog(value = "选课模式-通过id查询")
	@ApiOperation(value="通过id查询", notes="选课模式-通过id查询")
	@PostMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VeJwXkms veJwXkms = veJwXkmsService.getById(id);
		if(veJwXkms==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(veJwXkms);
	}
	
	/**
	 * 新增信息
	 * @param veJwXkms
	 * @return
	 */
	@AutoLog(value = "选课模式-新增信息")
	@ApiOperation(value="新增信息", notes="选课模式-新增信息")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VeJwXkms veJwXkms) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(sysUser!=null){
            veJwXkms.setCreateuserid(sysUser.getId());
            veJwXkms.setCreateusername(sysUser.getRealname());
        }
        veJwXkms.setCreatetime(new Date());
		if(veJwXkmsService.save(veJwXkms)){
		   return Result.OK("添加成功！",veJwXkms.getId());
        }
        return Result.error("添加失败!");
	}
	
	/**
	 * 修改信息
	 * @param veJwXkms
	 * @return
	 */
	@AutoLog(value = "选课模式-修改信息")
	@ApiOperation(value="修改信息", notes="选课模式-修改信息")
	@PostMapping(value = "/edit")
	public Result<?> edit(@RequestBody VeJwXkms veJwXkms) {
        veJwXkms.setUpdatetime(new Date());
        if(veJwXkmsService.updateById(veJwXkms)){
		    return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败!");
	}
	
//	/**
//	 * 通过id删除
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "选课模式-通过id删除")
//	@ApiOperation(value="通过id删除", notes="选课模式-通过id删除")
//	@PostMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//        if(veJwXkmsService.removeById(id)){
//		    return Result.OK("删除成功!");
//        }
//        return Result.error("删除失败!");
//	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "选课模式-批量销毁")
	@ApiOperation(value="批量销毁", notes="选课模式-批量销毁")
	@PostMapping(value = "/deleteBatch")
	public Result<?> removeBatch(@RequestParam(name="ids",required=true) String ids) {
		if(this.veJwXkmsService.removeByIds(Arrays.asList(ids.split(",")))){
		         return Result.OK("批量销毁成功!");
        }
        return Result.error("批量销毁失败!");
	}

     /**
      * 发布信息
      * @param ids
      * @return
      */
     @AutoLog(value = "调课信息-批量删除")
     @PostMapping("deleteByStatus")
     @ApiOperation(value = "批量删除", notes = "调课信息-批量删除")
     public Result<?> deleteByStatus(@RequestParam(name="ids",required=true) String ids)
     {
         if(this.veJwXkmsService.changeDelStatusByIds(ids.split(","),1)>0){
             return Result.OK("操作成功!");
         }
         return Result.error("操作失败");
     }

     /**
      * 取消发布信息
      * @param ids
      * @return
      */
     @AutoLog(value = "调课信息-批量还原")
     @PostMapping("reverseByStatus")
     @ApiOperation(value = "批量还原", notes = "调课信息-批量还原")
     public Result<?> reverseByStatus(@RequestParam(name="ids",required=true) String ids)
     {
         if(this.veJwXkmsService.changeDelStatusByIds(ids.split(","),0)>0){
             return Result.OK("操作成功!");
         }
         return Result.error("操作失败");
     }

}
